﻿Chenxin.UI.CustomerInfoAdd = Chenxin.extend(Chenxin.UI.PageCommon,{
    actionUrl:'service/customerMsgr.ashx',
    createForm:function(){
        
        //客户基本信息
		if($("#ctl00_main_hidBaseInfo").val() != ""){
			var jsonBaseInfo = eval( "(" + $("#ctl00_main_hidBaseInfo").val() + ")");
			return new Chenxin.UI.CustomerBaseInfoForm({
				initData:jsonBaseInfo,
				op:"edit",
				type:'numal',
			});
	    //客户需求信息
		}else if($("#ctl00_main_hidDemandInfo").val() != ""){
			var jsonDemandInfo = eval( "(" + $("#ctl00_main_hidDemandInfo").val() + ")");
			return new Chenxin.UI.CustomerDemandInfoForm({
				initData:jsonDemandInfo,
				op:"edit",
				type:'numal'
			});
		//客户信息
		}else{
		    $("#jqmMsgBox").jqm({ trigger: false}).jqDrag('.jqDrag');
			return new Chenxin.UI.CustomerInfoAddForm({
			    scope:this,
				type:'numal'
			});
		}
	},
	initEvent:function(){
	    var objPage = this;
        /****************事件：继续添加****************/
        $("#aContinue").click(function(){
//            $('#jqmMsgBox').jqmHide();
//            objPage.form.showContentTable(10,10);
            document.location.href = "/CustomerInfoAdd.aspx";
        });
        
		/****************事件：重置事件****************/
	    $("#aReturn").click(function(){
	        //objPage.form.reset();
	        document.location.href = "/CustomerMsgr.aspx";
	    });
	    /****************事件：提交事件****************/
	    $("#aSubmit").click(function(){
	        //需要登录
	        var bNeedAdd = true;
	        //客需登录
	        if( objPage.form.initData == undefined ){
	            //私有客需
	            if($("input[name='DemandInfo_PubLevel']:checked").val() == "0"){
	                Chenxin.ajax({
                        type: "get",
                        dataType: "text",
                        paramType: 'json',
                        url: objPage.actionUrl,
                        data: { op: 'chkover'},
                        async : false,
                        onSuccess: function(data, msg) {
                            if( data > 0 ){
                                if(!confirm("您已登记了" + data + "条私有客户信息，新登记的客户及需求将被公开到本部门共享池中。是否确定继续添加此客户客需？" )){
                                    bNeedAdd =false;
                                }else{
                                    objPage.isDepPub = true;
                                }
                            }
                        }
                    });
	            }
	        }
	        if( bNeedAdd ){
	            objPage.form.save();
	        }
	    });
	},
	initComponent: function() {
        Chenxin.UI.CustomerInfoAdd.superclass.initComponent.call(this);
    }
});

/**
*添加客需表单
**/
Chenxin.UI.CustomerInfoAddForm = Chenxin.extend(Chenxin.UI.FormCommon,{
    actionUrl:'service/customerMsgr.ashx',
	type:'numal',
	id:'divCustomer',
	initFormValidator:function(){
	    var frm = this;
		//客户姓名非空验证
        $("#txtCustomerName").formValidator({ 
			validatorgroup:this.id,
            onshow: "请输入客户姓名！", onfocus: "请输入客户姓名", oncorrect: " " 
        }).inputValidator({ 
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "客户姓名不能为空!" }, onerror: "客户姓名不能为空!" 
        });
        //身份证
        $("#txtCertificateNo").formValidator({
            validatorgroup:this.id,
            onshow:"请输入证件号！",onfocus:"请输入证件号",oncorrect:" "
        }).functionValidator({
            fun:function(val,elem){
                if(frm.cmbIDType.getValue() == 10){
                    if( val != ""){
                        if( !val.isIdCardNo() ){
                            return "身份证格式不正确！";
                        }
                    }
                }else if(frm.cmbIDType.getValue() != "请选择"){
                    if( val == ""){
                        return "请输入相应的证件号！";
                    }
                }
                return true;
            }
        });
        
        //手机
        $("#txtMobile").formValidator({
            validatorgroup:this.id,
            empty:true,
            onshow:"手机和电话号码二选一必填！",
            onfocus:"你要是输入了，必须输入正确",
            onempty:"你真的不想留手机号码吗？",
            oncorrect:"输入正确"
        }).inputValidator({
            min:11,
            max:11,
            onerror:"手机号码必须是11位的,请确认"
        }).regexValidator({
            regexp:"mobile",
            datatype:"enum",
            onerror:"你输入的手机号码格式不正确"
        });
        
        //电话
        $("#txtPhone").formValidator({
            validatorgroup:this.id,
            //empty:true,
            onshow:"手机和电话号码二选一必填！",
            onfocus:"格式例如：(国际区号-)(国内区号-)号码(-分机号)",
            //onempty:"你真的不想留电话吗？",
            oncorrect:"输入正确"
        }).functionValidator({
            fun:function(val,elem){
                if( $("#txtMobile").val() == "" && val == "" ){
                    return "手机和电话号码二选一必填！";
                }else{
                    return true;
                }
            }
        });
        
	},
	createComboList:function(){
	    //国籍
	    this.cmbNationality = new Chenxin.UI.ParameterCombo({
			containerId: '#CustomerInfo_Nationality',
			name: 'CustomerInfo_Nationality',
			key:'Custom.Dationality',
			inModelType:'txt',
			tbxWidth:120,
			required:true
		});
	    //证件类型
		this.cmbIDType=new Chenxin.UI.ParameterCombo({
			containerId: '#CustomerInfo_CertificateType',
			name: 'CustomerInfo_CertificateType',
			key:'Custom.CertificateType',
			inModelType:'txt',
			tbxWidth:120,
			required:false
		});
		////求购
		//房间类型需求
		this.cmbBuyHouseRoom = new Chenxin.UI.Combo({
            containerId: '#divBuyHouseRoom',
            tbxWidth: 120,
            items: comboCommon.RoomCount
        });
        //厅房类型需求
		this.cmbBuyHouseHall = new Chenxin.UI.Combo({
            containerId: '#divBuyHouseHall',
            tbxWidth: 120,
            items: comboCommon.RoomCount
        });
        //卫生间类型需求
		this.cmbBuyHouseToilet = new Chenxin.UI.Combo({
            containerId: '#divBuyHouseToilet',
            tbxWidth: 120,
            items: comboCommon.RoomCount
        });
        ////求租
        //房间类型需求
		this.cmbRentHouseRoom = new Chenxin.UI.Combo({
            containerId: '#divRentHouseRoom',
            tbxWidth: 120,
            items: comboCommon.RoomCount
        });
        //厅房类型需求
		this.cmbRentHouseHall = new Chenxin.UI.Combo({
            containerId: '#divRentHouseHall',
            tbxWidth: 120,
            items: comboCommon.RoomCount
        });
        //卫生间类型需求
		this.cmbRentHouseToilet = new Chenxin.UI.Combo({
            containerId: '#divRentHouseToilet',
            tbxWidth: 120,
            items: comboCommon.RoomCount
        });
        this.comboListDemandInfo = [this.cmbBuyHouseRoom,this.cmbBuyHouseHall,this.cmbBuyHouseToilet,this.cmbRentHouseRoom,this.cmbRentHouseHall,this.cmbRentHouseToilet];
        this.comboListCustomerInfo = [this.cmbNationality,this.cmbIDType];
		return [this.cmbNationality,this.cmbIDType,this.cmbBuyHouseRoom,this.cmbBuyHouseHall,this.cmbBuyHouseToilet,this.cmbRentHouseRoom,this.cmbRentHouseHall,this.cmbRentHouseToilet];
	},
	initEvent:function(){
	    var thisFrm = this;
	    /****************事件：求租****************/
	    $("#radBuy").click(function(){
	        var DemandType = $("input[name='DemandInfo_DemandType']").val();
	        thisFrm.showContentTable(this.value,DemandType);
	    });
	    /****************事件：求购****************/
	    $("#radRental").click(function(){
	        var DemandType = $("input[name='DemandInfo_DemandType']").val();
	        thisFrm.showContentTable(this.value,DemandType);
	    });
	    /****************事件：住房****************/
	    $("#radHouse").click(function(){
	        var CustomerKind = $("input[name='DemandInfo_DemandKind']").val();
	        thisFrm.showContentTable(CustomerKind,this.value);
	    });
	    /****************事件：商铺****************/
	    $("#radShop").click(function(){
	        var CustomerKind = $("input[name='DemandInfo_DemandKind']").val();
	        thisFrm.showContentTable(CustomerKind,this.value);
	    });
	    /****************事件：办公****************/
	    $("#radOffice").click(function(){
	        var CustomerKind = $("input[name='DemandInfo_DemandKind']").val();
	        thisFrm.showContentTable(CustomerKind,this.value);
	    });
	},
	saveSucc:function(data,msg){
	    //客源编号
	    $("#spanCustomerCode").text(data);
	    $('#jqmMsgBox').jqmShow();
	},
	showContentTable:function(CustomerKind,DemandType){
	    $("#tbBuyHouse").hide();
	    $("#tbBuyShop").hide();
	    $("#tbBuyOffice").hide();
	    $("#tbRentHouse").hide();
	    $("#tbRentShop").hide();
	    $("#tbRentOffice").hide();
	    switch(CustomerKind.toString() + DemandType.toString()){
            case "1010":
                $("#tbBuyHouse").show();
                break;
            case "1020":
                $("#tbBuyShop").show();
                break;
            case "1030":
                $("#tbBuyOffice").show();
                break;
            case "2010":
                $("#tbRentHouse").show();
                break;
            case "2020":
                $("#tbRentShop").show();
                break;
            case "2030":
                $("#tbRentOffice").show();
                break;
	    }
	    //全部清除
	    Chenxin.Fn.setFormValue("divDemandDetailInfo",this.comboListDemandInfo,{});
	},
	afterGetFormJson:function(json){    
	    //求购
	    //房型需求-室
	    if(json.divBuyHouseRoom != undefined){
	        json.DemandInfo_DemandStyle_Room = json.divBuyHouseRoom;
	        delete json.divBuyHouseRoom;
	    }
	    //房型需求-厅
	    if(json.divBuyHouseHall != undefined){
	        json.DemandInfo_DemandStyle_Hall = json.divBuyHouseHall;
	        delete json.divBuyHouseHall;
	    }
	    //房型需求-卫
	    if(json.divBuyHouseToilet != undefined){
	        json.DemandInfo_DemandStyle_Toilet = json.divBuyHouseToilet;
	        delete json.divBuyHouseToilet;
	    }
	    //求租
	    //房型需求-室
	    if(json.divRentHouseRoom != undefined){
	        json.DemandInfo_DemandStyle_Room = json.divRentHouseRoom;
	        delete json.divRentHouseRoom;
	    }
	    //房型需求-厅
	    if(json.divRentHouseHall != undefined){
	        json.DemandInfo_DemandStyle_Hall = json.divRentHouseHall;
	        delete json.divRentHouseHall;
	    }
	    //房型需求-卫
	    if(json.divRentHouseToilet != undefined){
	        json.DemandInfo_DemandStyle_Toilet = json.divRentHouseToilet;
	        delete json.divRentHouseToilet;
	    }
	    var strRemark = $("textarea[name='DemandInfo_Remark']").text();
	    if( strRemark != ""){
	        json.DemandInfo_Remark = strRemark;
	    }
	    //部门级公开
	    if(this.isDepPub){
	        json.DemandInfo_PubLevel = 30;
	    }
	},
	initComponent:function(){
		Chenxin.UI.CustomerInfoAddForm.superclass.initComponent.call(this);
	}
});

/**
*更新客户基本信息表单
**/
Chenxin.UI.CustomerBaseInfoForm = Chenxin.extend(Chenxin.UI.FormCommon,{
	actionUrl:'service/customerMsgr.ashx?type=BaseInfo',
	type:'numal',
	id:'divBaseInfo',
	initFormValidator:function(){
	    var frm = this;
		//客户姓名非空验证
        $("#txtCustomerName").formValidator({ 
			validatorgroup:this.id,
            onshow: "请输入客户姓名！", onfocus: "请输入客户姓名", oncorrect: " " 
        }).inputValidator({ 
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "客户姓名不能为空!" }, onerror: "客户姓名不能为空!" 
        });
         //身份证
        $("#txtCertificateNo").formValidator({
            validatorgroup:this.id,
            onshow:"请输入证件号！",onfocus:"请输入证件号",oncorrect:" "
        }).functionValidator({
            fun:function(val,elem){
                if(frm.cmbIDType.getValue() == 10){
                    if( val != ""){
                        if( !val.isIdCardNo() ){
                            return "身份证格式不正确！";
                        }
                    }
                }
                return true;
            }
        });
        
        //手机
        $("#txtMobile").formValidator({
            validatorgroup:this.id,
            empty:true,
            onshow:"手机和电话号码二选一必填！",
            onfocus:"你要是输入了，必须输入正确",
            onempty:"你真的不想留手机号码吗？",
            oncorrect:"输入正确"
        }).inputValidator({
            min:11,
            max:11,
            onerror:"手机号码必须是11位的,请确认"
        }).regexValidator({
            regexp:"mobile",
            datatype:"enum",
            onerror:"你输入的手机号码格式不正确"
        });
        
        //电话
        $("#txtPhone").formValidator({
            validatorgroup:this.id,
            //empty:true,
            onshow:"手机和电话号码二选一必填！",
            onfocus:"格式例如：(国际区号-)(国内区号-)号码(-分机号)",
            //onempty:"你真的不想留电话吗？",
            oncorrect:"输入正确"
        }).functionValidator({
            fun:function(val,elem){
                if( $("#txtMobile").val() == "" && val == "" ){
                    return "手机和电话号码二选一必填！";
                }else{
                    return true;
                }
            }
        });
	},
	createComboList:function(){
	    //国籍
	    this.cmbNationality = new Chenxin.UI.ParameterCombo({
			containerId: '#CustomerInfo_Nationality',
			name: 'CustomerInfo_Nationality',
			key:'Custom.Dationality',
			inModelType:'txt',
			initValue:this.initData.CustomerInfo_Nationality,
			tbxWidth:120,
			required:true,
			onSelected:function(){
				
			}
		});
	    //证件类型
		this.cmbIDType=new Chenxin.UI.ParameterCombo({
			containerId: '#CustomerInfo_CertificateType',
			name: 'CustomerInfo_CertificateType',
			key:'Custom.CertificateType',
			inModelType:'txt',
			initValue:this.initData.CustomerInfo_CertificateType,
			tbxWidth:120,
			required:false,
			onSelected:function(){
				
			}
		});
        return [this.cmbNationality,this.cmbIDType];
	},
	afterGetFormJson:function(json){
        //json.CustomerId = $("#ctl00_main_CustomerId").val();
	},
	saveSucc:function(data,msg){
		window.location.href = "/CustomerMsgr.aspx";
	},
	initComponent:function(){
		Chenxin.UI.CustomerBaseInfoForm.superclass.initComponent.call(this);
		if(this.initData){
		    Chenxin.Fn.setFormValue(this.id,[],this.initData);
		}
	}
});

/**
*更新客户需求信息表单
**/
Chenxin.UI.CustomerDemandInfoForm = Chenxin.extend(Chenxin.UI.FormCommon,{
	actionUrl:'service/customerMsgr.ashx?type=DemandInfo',
	type:'numal',
	id:'divDemandInfo',
	createComboList:function(){
	    ////求购
		//房间类型需求
		var cmbBuyHouseRoom = new Chenxin.UI.Combo({
            containerId: '#divBuyHouseRoom',
            tbxWidth: 120,
            items: comboCommon.RoomCount
        });
        //厅房类型需求
		var cmbBuyHouseHall = new Chenxin.UI.Combo({
            containerId: '#divBuyHouseHall',
            tbxWidth: 120,
            items: comboCommon.RoomCount
        });
        //卫生间类型需求
		var cmbBuyHouseToilet = new Chenxin.UI.Combo({
            containerId: '#divBuyHouseToilet',
            tbxWidth: 120,
            items: comboCommon.RoomCount
        });
        ////求租
        //房间类型需求
		var cmbRentHouseRoom = new Chenxin.UI.Combo({
            containerId: '#divRentHouseRoom',
            tbxWidth: 120,
            items: comboCommon.RoomCount
        });
        //厅房类型需求
		var cmbRentHouseHall = new Chenxin.UI.Combo({
            containerId: '#divRentHouseHall',
            tbxWidth: 120,
            items: comboCommon.RoomCount
        });
        //卫生间类型需求
		var cmbRentHouseToilet = new Chenxin.UI.Combo({
            containerId: '#divRentHouseToilet',
            tbxWidth: 120,
            items: comboCommon.RoomCount
        });
        this.buyComboList = [cmbBuyHouseRoom,cmbBuyHouseHall,cmbBuyHouseToilet];
        this.rentComboList = [cmbRentHouseRoom,cmbRentHouseHall,cmbRentHouseToilet];
		return [cmbBuyHouseRoom,cmbBuyHouseHall,cmbBuyHouseToilet,cmbRentHouseRoom,cmbRentHouseHall,cmbRentHouseToilet];
	},
	afterGetFormJson:function(json){    
	    //求购
	    //房型需求-室
	    if(json.divBuyHouseRoom != undefined){
	        json.DemandInfo_DemandStyle_Room = json.divBuyHouseRoom;
	        delete json.divBuyHouseRoom;
	    }
	    //房型需求-厅
	    if(json.divBuyHouseHall != undefined){
	        json.DemandInfo_DemandStyle_Hall = json.divBuyHouseHall;
	        delete json.divBuyHouseHall;
	    }
	    //房型需求-卫
	    if(json.divBuyHouseToilet!= undefined){
	        json.DemandInfo_DemandStyle_Toilet = json.divBuyHouseToilet;
	        delete json.divBuyHouseToilet;
	    }
	    //求租
	    //房型需求-室
	    if(json.divRentHouseRoom != undefined){
	        json.DemandInfo_DemandStyle_Room = json.divRentHouseRoom;
	        delete json.divRentHouseRoom;
	    }
	    //房型需求-厅
	    if(json.divRentHouseHall != undefined){
	        json.DemandInfo_DemandStyle_Hall = json.divRentHouseHall;
	        delete json.divRentHouseHall;
	    }
	    //房型需求-卫
	    if(json.divRentHouseToilet != undefined){
	        json.DemandInfo_DemandStyle_Toilet = json.divRentHouseToilet;
	        delete json.divRentHouseToilet;
	    }
	    var strRemark = $("textarea[name='DemandInfo_Remark']").text();
	    if(strRemark != ""){
	        json.DemandInfo_Remark = strRemark;
	    }
	    //json.CustomerId = $("#ctl00_main_CustomerId").val();
	},
	initEvent:function(){
		var thisFrm = this;
	    /****************事件：求租****************/
	    $("#radBuy").click(function(){
	        var DemandType = $("input[name='DemandInfo_DemandType']").val();
	        thisFrm.showContentTable(this.value,DemandType);
	    }); 
	    /****************事件：求购****************/
	    $("#radRental").click(function(){
	        var DemandType = $("input[name='DemandInfo_DemandType']").val();
	        thisFrm.showContentTable(this.value,DemandType);
	    });
	    /****************事件：住房****************/
	    $("#radHouse").click(function(){
	        var CustomerKind = $("input[name='DemandInfo_DemandKind']:checked").val();
	        
	        thisFrm.showContentTable(CustomerKind,this.value);
	    });
	    /****************事件：商铺****************/
	    $("#radShop").click(function(){
	        var CustomerKind = $("input[name='DemandInfo_DemandKind']:checked").val();
	        thisFrm.showContentTable(CustomerKind,this.value);
	    });
	    /****************事件：办公****************/
	    $("#radOffice").click(function(){
	        var CustomerKind = $("input[name='DemandInfo_DemandKind']:checked").val();
	        thisFrm.showContentTable(CustomerKind,this.value);
	    });
	},
	saveSucc:function(data,msg){
		window.location.href = "/CustomerMsgr.aspx";
	},
	showContentTable:function(CustomerKind,DemandType){
	    $("#tbBuyHouse").hide();
	    $("#tbBuyShop").hide();
	    $("#tbBuyOffice").hide();
	    $("#tbRentHouse").hide();
	    $("#tbRentShop").hide();
	    $("#tbRentOffice").hide();
	    switch(CustomerKind.toString() + DemandType.toString()){
            case "1010":
                $("#tbBuyHouse").show();
                break;
            case "1020":
                $("#tbBuyShop").show();
                break;
            case "1030":
                $("#tbBuyOffice").show();
                break;
            case "2010":
                $("#tbRentHouse").show();
                break;
            case "2020":
                $("#tbRentShop").show();
                break;
            case "2030":
                $("#tbRentOffice").show();
                break;
	    }
	    //全部清除
	    Chenxin.Fn.setFormValue("divDemandDetailInfo",this.comboList,{});
	},
	initComponent:function(){
		Chenxin.UI.CustomerDemandInfoForm.superclass.initComponent.call(this);
		if(this.initData){
		    var curForm;
		    var comboList = [];
		    switch(this.initData.DemandInfo_DemandKind.toString() + this.initData.DemandInfo_DemandType.toString()){
		        case "1010":
                    curForm = "tbBuyHouse";
                    comboList = this.buyComboList;
                    break;
                case "1020":
                    curForm = "tbBuyShop";
                    break;
                case "1030":
                    curForm = "tbBuyOffice";
                    break;
                case "2010":
                    curForm = "tbRentHouse";
                    comboList = this.rentComboList;
                    break;
                case "2020":
                    curForm = "tbRentShop";
                    break;
                case "2030":
                    curForm = "tbRentOffice";
                    break;
		    }
		    Chenxin.Fn.setFormValue(curForm,comboList,this.initData);
		    Chenxin.Fn.setFormValue("divDemandType",[],this.initData);
		    Chenxin.Fn.setFormValue("tbAllowPub",[],this.initData);
		}
	}
});

$(document).ready(function(){
    var customerInfoAdd = new Chenxin.UI.CustomerInfoAdd();
});